from appium import webdriver
from appium.options.android import UiAutomator2Options
from selenium.webdriver.common.action_chains import ActionChains
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from time import sleep

desired_caps = {
    "platformName": "Android",
    "platformVersion": "13",
    "deviceName": "Genymotion Virtual Device",
    "appPackage": "com.android.settings",
    "appActivity": ".Settings",
}

options=UiAutomator2Options().load_capabilities(desired_caps)
driver=webdriver.Remote(
    command_executor='http://127.0.0.1:4723/wd/hub',
    options=options)

try:
    # 等待元素加载
    element = WebDriverWait(driver, 10).until(
        EC.element_to_be_clickable((By.XPATH, '//*[@text="Storage"]'))
    )

    # 使用 ActionChains 执行长按
    actions = ActionChains(driver)
    actions.click_and_hold(element).perform()  # 长按操作
    sleep(3)  # 保持长按3秒
    actions.release().perform()  # 释放

except Exception as e:
    print(f"Error: {e}")
finally:
    driver.quit()